package com.v3webservice.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.v3webservice.util.WebServiceUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.ParseException;
import java.util.*;

public class V3webservice {
    public synchronized static String accessService( Map<String, String> params, String result) throws Exception{

//        String method = resourceBundle.getString("method");
//        String ns = resourceBundle.getString("ns");
//        String docType = resourceBundle.getString("docType");

//        String a = "MTF21010700013,MTF21010600005,MTF21010700014,MTF21010700015,MTF21010900005,MTF21010900006,MTF21010900008,MTF21011000002,MTF21011100002,MTF21011200009,MTF21011400001,MTF21011300003,MTF21011600002,MTF21011600008,MTF21011800002,MTF21011800010,MTF21011900003,MTF21011900004,MTF21011900013,MTF21012100006,MTF21010800012,MTF21011300001,MTF21010600014,MTF21011200012,MTF21011300010,MTF21011400005,MTF21011500003,MTF21011300004,MTF21011300005,MTF21011300006,MTF21011600007,MTF21011700001,MTF21011800007,MTF21012100002,MTF21012200005,MTF21012200013,MTF21010500010,MTF21010700005,MTF21010700016,MTF21010800010,MTF21010800001,MTF21010800011,MTF21010900009,MTF21010900010,MTF21011000001,MTF21011100012,MTF21011200002,MTF21011200011,MTF21011300002,MTF21011400002,MTF21011400003,MTF21011900010,DMTF21011900001,MTF21010700012,MTF21011100011,MTF21011100001,MTF21010500011,MTF21011100014,MTF21011100015,MTF21011300008,MTF21011300009,MTF21011400008,DMTF21011500001,MTF21011500009,MTF21011500006,MTF21011600001,MTF21011700003,MTF21011700002,MTF21011900002,DMTF21011900002,MTF21011900011,MTF21011900012,MTF21010600006,MTF21010600013,MTF21010900007,MTF21011400007,MTF21011500001,MTF21011500002,DMTF21011300002,DMTF21011300001,MTF21011600003,MTF21011600004,MTF21011900008,MTF21012000003,MTF21012000004,MTF21012000005,MTF21012000002,MTF21012000010,MTF21012000015,MTF21012000017,MTF21012100005,MTF21012100004,DMTF21012100002,MTF21012100007,MTF21012100008,MTF21012200002";
//        String[] array = a.split(",");
//        for(String arr : array){
//            get(arr);
//        }
        String a = "ALLOT1022022032300005";
        get(a);
        return null;
    }

    public static void get(String allotId) throws IOException, ParseException {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("v3webservice");
        String wsdl = resourceBundle.getString("wsdl");
        String docType = "GETALLOTSTAT";
        String inputXml1 = "{\"AllotId\":\""+allotId+"\"}";
        StringBuffer stringBuffer = new StringBuffer("");
        stringBuffer.append("<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
        stringBuffer.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" );
        stringBuffer.append(" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" );
        stringBuffer.append(" xmlns:urn=\"urn:WebServiceInf-IWebService\">");
        stringBuffer.append("<soapenv:Body>");
        stringBuffer.append("<urn:getIMSData soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
        stringBuffer.append("<DocType xsi:type=\"xsd:string\">"+docType+"</DocType>");
        stringBuffer.append("<InputDTOXml xsi:type=\"xsd:string\">"+inputXml1+"</InputDTOXml>");
        stringBuffer.append("<ResultDTOXml xsi:type=\"xsd:string\">?</ResultDTOXml>");//数据批次编号
        stringBuffer.append("<ResultMsg xsi:type=\"xsd:string\">?</ResultMsg>");
        stringBuffer.append("</urn:getIMSData>");
        stringBuffer.append("</soapenv:Body>");
        stringBuffer.append("</soapenv:Envelope>");

        HttpPost httpPost = new HttpPost(wsdl);
        httpPost.setHeader("Context-Type","text/xml;charset=UTF-8");
//        httpPost.setHeader("SOAPAction", "urn:WebServiceInf-IWebService#getIMSData");
//        httpPost.setHeader("Connection","Keep-Alive");
        RequestConfig config = RequestConfig.custom().setSocketTimeout(5000000).setConnectTimeout(5000000).build();
        httpPost.setConfig(config);
        StringEntity data = new StringEntity(String.valueOf(stringBuffer), "UTF-8");
        data.setContentType("text/xml;charset=UTF-8");

        httpPost.setEntity(data);
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient httpClient = httpClientBuilder.build();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        if(response != null){
            String responseEntity = EntityUtils.toString(response.getEntity(),"UTF-8");
            if(responseEntity != null){
//                System.out.println(responseEntity);
                JSONArray array = WebServiceUtils.xmlToJson(Jsoup.parse(responseEntity)).getJSONArray("data");
//                System.out.println(array);
            }
        }
    }

    public static JSONObject xmlToJson(org.jsoup.nodes.Element newdataset)
    {
        JSONObject objJson = new JSONObject();
        Elements children = newdataset.getElementsByTag("ResultDTOXml");
        List list = new ArrayList();
        for (int i = 0; i < children.size(); i++) {
            Map map = new HashMap();
            org.jsoup.nodes.Element child = (org.jsoup.nodes.Element)children.get(i);
            Elements allEls = child.getAllElements();
            for (int j = 1; j < allEls.size(); j++) {
                org.jsoup.nodes.Element els = (org.jsoup.nodes.Element)allEls.get(j);
                String nodeName = els.nodeName();
                String value = els.html();

                for (int k = 0; k < nodeName.length(); k++) {
                    if ((nodeName.charAt(k) == '_') && (k < nodeName.length())) {
                        nodeName = nodeName.replace(nodeName.substring(k, k + 2),
                                nodeName.substring(k + 1, k + 2).toUpperCase());
                    }
                }
                map.put(nodeName, value);
            }
            list.add(map);
        }
        objJson.put("data", list);
        return objJson;
    }
}
